package com.dhcc.core.modules.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.dhcc.core.framework.constant.state.ResourceStatus;
import com.dhcc.core.framework.exception.BizException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.constant.Const;
import com.dhcc.core.framework.result.node.ZTreeNodeMenu;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.dao.ResourceMapper;
import com.dhcc.core.modules.system.entity.Resource;
import com.dhcc.core.modules.system.service.IResourceService;

/**
 * 资源服务
 * 
 * @ClassName: ResourceServiceImpl
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:40:57
 */
@Service
public class ResourceServiceImpl extends BaseServiceImpl<ResourceMapper, Resource> implements IResourceService {

    @Override
    public void delResource(Long resourceId) {
        
        // 删除关联的relation
        this.baseMapper.deleteRelationByResource(resourceId);

        // 删除资源
        this.baseMapper.deleteById(resourceId);

    }

    @Override
    @Transactional
    public void delResourceContainSubResources(Long resourceId) {

        Resource resource = baseMapper.selectById(resourceId);

        // 删除所有子菜单
        Wrapper<Resource> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("path", "%[" + resource.getId() + "]%");
        List<Resource> resources = baseMapper.selectList(wrapper);
        for (Resource temp : resources) {
            delResource(temp.getId());
        }

        // 删除当前菜单
        delResource(resourceId);
    }

    @Override
    public List<Map<String, Object>> selectResources(Integer resGroup, String condition, String level) {
        return this.baseMapper.selectResources(resGroup, condition, level);
    }

    @Override
    public List<Long> getResourceIdsByPrincipalId(Long roleId,Integer principalType) {
        return this.baseMapper.getResourceIdsByPrincipalId(roleId,principalType);
    }

    @Override
    public List<ZTreeNode> tree(Integer resGroup) {
        List<ZTreeNode> tree = this.baseMapper.tree(resGroup);
        tree.add(ZTreeNode.createRoot());
        return tree;
    }

    @Override
    public List<ZTreeNode> resourceTreeListByResourceIds(Integer resGroup, List<Long> resourceIds) {
        return this.baseMapper.resourceTreeListByResourceIds(resGroup, resourceIds);
    }

    @Override
    public int deleteRelationByResource(Long resourceId) {
        return this.baseMapper.deleteRelationByResource(resourceId);
    }

    @Override
    public List<String> getResPermsByPrincipalId(Long principalId) {
        return this.baseMapper.getResPermsByPrincipalId(principalId);
    }

    @Override
    public List<ZTreeNodeMenu> getUserMenus(Long userId,List<Long> deptIds,List<Long> roleIds) {
        if(CommonUtil.isEmpty(roleIds)){
            roleIds = new ArrayList<Long>();
        }
        if(CommonUtil.isEmpty(deptIds)){
            deptIds = new ArrayList<Long>();
        }
        roleIds.add(Const.EVERYONE_ROLE_ID);//everyone角色
        return this.baseMapper.getUserMenus(userId,deptIds,roleIds);
    }

    @Override
    public void changeStatus(Long id) {
        Resource r = this.baseMapper.selectById(id);
        if (CommonUtil.isNotEmpty(r)) {
            this.baseMapper.changeStatus(id,
                    (r.getStatus()!=null && ResourceStatus.ENABLE.getCode() == r.getStatus()) ? ResourceStatus.DISABLE.getCode()
                            : ResourceStatus.ENABLE.getCode());
        } else {
            throw new BizException("资源不存在...");
        }
    }
}
